package com.example.case2;

import lombok.SneakyThrows;
import org.apache.zookeeper.KeeperException;

import java.io.IOException;

/**
 * @ClassName DistributeLock
 * @Author 小坏
 * @Date 2021/8/22、19:43
 * @Version 1.0
 */
public class DistributeLockTest {
    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        final DistributeLock lock1 = new DistributeLock();
        final DistributeLock lock2 = new DistributeLock();


            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {

                    try {
                        //上锁
                        lock1.zkLock();
                        Thread.sleep(5 * 1000);
                        System.out.println("线程1，启动 获取锁");
                        lock1.unLock();
                        System.out.println("lock1 释放锁");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();


        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {

                try {
                    //上锁
                    lock2.zkLock();
                    Thread.sleep(5 * 1000);
                    System.out.println("线程2，启动 获取锁");
                    lock2.unLock();
                    System.out.println("lock2 释放锁");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}
